package com.chixing.service.impl;


import com.chixing.mapper.*;
import com.chixing.pojo.*;
import com.chixing.service.ProductService;
import com.chixing.util.ServerResult;
import com.chixing.vo.ProductDetailVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProductServiceImpl implements ProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private ProductPicInfoMapper productPicInfoMapper;

    @Override
    public ServerResult getProDetailById(Integer productId) {


        ProductDetailVO vo=new ProductDetailVO();
        Product product=productMapper.selectByPrimaryKey(productId);
        System.out.println("service.......获得id:"+productId);
        System.out.println("service.......获得product:"+product);
        //select * from dish where shop_id = ?
        ProductExample productExample=new ProductExample();

        productExample.createCriteria().andProductIdEqualTo(productId);
        //select * from review where shop_id = ?
        CommentExample commentExample=new CommentExample();
        commentExample.setOrderByClause("comment_time desc ");
        List<Comment> commentList=commentMapper.selectByExample(commentExample);
        Map<Comment, Customer> commentCustomerMap=new HashMap<>();
        if(commentList!=null && commentList.size()>0){
            for(Comment comment:commentList){
                //select * from customer where custmer_id = ?
                Customer customer=customerMapper.selectByPrimaryKey(comment.getCustomerId());
                commentCustomerMap.put(comment,customer);
            }
        }

        //查询商品所有图片
       ProductPicInfoExample productPicInfoExample = new ProductPicInfoExample();
       ProductPicInfoExample.Criteria criteria = productPicInfoExample.createCriteria();
       criteria.andProductIdEqualTo(productId);
       List<ProductPicInfo> productPicInfos = productPicInfoMapper.selectByExample(productPicInfoExample);

         //查询商品的所有规格
        Product product1 = productMapper.selectByPrimaryKey(productId);
        ProductExample productExample1 = new ProductExample();
        ProductExample.Criteria criteria1 = productExample1.createCriteria();
        criteria1.andProductNameEqualTo(product1.getProductName());
        List<Product> products = productMapper.selectByExample(productExample1);

        //创建一个list用于存放商品规格信息。
        ArrayList<Double> specs = new ArrayList<>();
        for (Product p:products
             ) {
          if (!specs.contains(p.getProductSpecs())) {
            specs.add(p.getProductSpecs());
          }
        }


        vo.setProduct(product);
        vo.setReviewProduct(commentCustomerMap);
        vo.setProductPicInfo(productPicInfos);
        vo.setSpecs(specs);
          if(product!=null){
              System.out.println("vo" + vo);
              return ServerResult.getSuccess(vo);
          }
          return ServerResult.getFail(null);
    }

    @Override
    public ServerResult getById2AddReiew(Integer id) {
        ProductDetailVO productDetailVO=new ProductDetailVO();
        Product product=productMapper.selectByPrimaryKey(id);
        productDetailVO.setProduct(product);
        return ServerResult.getSuccess(productDetailVO);
    }

    @Override
    public ServerResult getByKeyWord(String keyword, Integer pageNum) {
    ProductExample productExample = new ProductExample();
    productExample.or().andProductNameLike("%" + keyword + "%");
    productExample.or().andProductMsgLike("%" + keyword + "%");
    productExample.setOrderByClause("product_price desc");

    //分页查询
    PageHelper.startPage(pageNum, 8);
    List<Product> products1 = productMapper.selectByExample(productExample);
    PageInfo<Product> productPageInfo = new PageInfo<>(products1);

    if (products1.size() != 0) {
      System.out.println("service---搜索--分页查询成功" + products1);
      return ServerResult.getSuccess(productPageInfo);
    }

    return ServerResult.getFail("查询失败。");
  }

    @Override
    public ServerResult getByConditions(String productBrand, Double minSpecs, Double maxSpecs, String productSupportAge, Double minPrice, Double maxPrice, Integer pageNum) {
        ProductExample productExample = new ProductExample();
        ProductExample.Criteria productCriteria = productExample.createCriteria();

        if (productBrand != null && productBrand.equals("-1"))
            productBrand = null;
        if (minSpecs != null && minSpecs == -1)
            minSpecs = null;
        if (maxSpecs != null && maxSpecs == -1)
            maxSpecs = null;
        if (productSupportAge != null && productSupportAge.equals("-1"))
            productSupportAge = null;
        if (minPrice != null && minPrice == -1)
            minPrice = null;
        if (maxPrice != null && maxPrice == -1)
            maxPrice = null;


        if (productBrand != null)
            productCriteria.andProductBrandEqualTo(productBrand);
        if (minSpecs != null)
            productCriteria.andProductPriceGreaterThanOrEqualTo(minSpecs);
        if (maxSpecs != null)
            productCriteria.andProductPriceLessThanOrEqualTo(maxSpecs);
        if (productSupportAge != null)
            productCriteria.andProductSupportAgeEqualTo(productSupportAge);
        if (minPrice != null)
            productCriteria.andProductPriceGreaterThanOrEqualTo(minPrice);
        if (maxPrice != null)
            productCriteria.andProductPriceLessThanOrEqualTo(maxPrice);


        System.out.println("productBrand:" + productBrand);
        System.out.println("minPrice:" + minSpecs);
        System.out.println("maxPrice:" + maxSpecs);

        System.out.println("minAge:" + productSupportAge);
        System.out.println("minPrice:" + minPrice);
        System.out.println("maxPrice:" + maxPrice);

        PageHelper.startPage(pageNum, 6);
        List<Product> productList = productMapper.selectByExample(productExample);
        PageInfo<Product> pageInfo = new PageInfo<>(productList);
        System.out.println("productList:" + productList);
        if (productList != null && productList.size() > 0) {
            return ServerResult.getSuccess(pageInfo);
        } else {
            return ServerResult.getFail(null);
        }
    }

    @Override
    public Product getbyid(Integer id) {
        return productMapper.selectByPrimaryKey(id);
    }

    @Override
    public ServerResult getByRecommen() {

       PageHelper.startPage(1, 8);
       List<Product> products = productMapper.selectByExample(null);
       PageInfo<Product> pageInfo = new PageInfo<>(products);
       return ServerResult.getSuccess(pageInfo);
    }

  @Override
  public ServerResult saveBlob(Integer proId, String editorData) {
    Product product = productMapper.selectByPrimaryKey(proId);


    byte[] bytes = editorData.getBytes();
    if (product != null) {
      product.setProductIntroduction(bytes);
      int i = productMapper.updateByPrimaryKeySelective(product);
      if (i > 0) return ServerResult.putSuccess(i);
    }

    return ServerResult.putFail(null);

  }

  @Override
  public ServerResult getProductInfo(Integer proId) {
    Product product = productMapper.selectByPrimaryKey(proId);
    byte[] productIntroduction = product.getProductIntroduction();
    //无法直接toString
    //String s = productIntroduction.toString();
    String s = new String(productIntroduction ,StandardCharsets.UTF_8);
    System.out.println("查询商品介绍成功!" + s);

    return ServerResult.getSuccess(s);
  }

}







